<%@ tag language="java" pageEncoding="UTF-8" body-content="empty" trimDirectiveWhitespaces="true"%>
<%@ attribute name="id" type="java.lang.String" required='true' description="树的id"%>
<%@ attribute name="ajax" type="java.lang.String" required='true' description="请求node节点的ajax的id" %>
<%@ attribute name="onCurrentNodeSelected" type="java.lang.String" description="绑定最近当前节点选中事件，onNodeSelected(node)" %>
<%@ attribute name="onMutiNodesSelected" type="java.lang.String" description="绑定多个节点选中事件，onMutiNodeSelected(nodes)" %>
<%@ attribute name="defaultSelectIndex" type="java.lang.Integer" description="设置默认选中第几项，default=0" %>
<%@ attribute name="showRemoveBtn" type="java.lang.Boolean" description="是否显示删除按钮，default=false" %>
<%@ attribute name="canEdit" type="java.lang.Boolean" description="是否可以编辑节点名称，default= false" %>
<%@ attribute name="checkable" type="java.lang.Boolean" description="是否显示复选框，default= false" %>
<%@ attribute name="selectedMulti" type="java.lang.Boolean" description="是否支持多选，default= false" %>
<%@ attribute name="showLine" type="java.lang.Boolean" description="是否显示对齐线条，default= false" %>
<%@ attribute name="dblClickExpand" type="java.lang.Boolean" description="是否支持双击展开，default= false" %>
<%@ attribute name="convertToTemplate" type="java.lang.String" description="将Ztree的单个node节点转为你所需要的node节点的函数，该函数需要返回新的节点，convertToTemplate(node,index)" %>
<%@ attribute name="nodeNameKey" type="java.lang.String" description="配置映射关系：设置节点名称对应在json中的哪个key，默认为text" %>
<%@ attribute name="nodeUrlKey" type="java.lang.String" description="配置映射关系：设置节点的url对应在json中的哪个key，默认为url" %>
<%@ attribute name="nodeCheckedKey" type="java.lang.String" description="配置映射关系：设置节点选中对应在json中的哪个key，默认为checked" %>
<%@ attribute name="nodeChildrenKey" type="java.lang.String" description="配置映射关系：设置节点的子节点对应在json中的哪个key，默认为children" %>
<%@ attribute name="nodeTitleKey" type="java.lang.String" description="配置映射关系：设置节点title对应在json中的哪个key，默认为title" %>
<%@ attribute name="maxLayer" type="java.lang.Integer" description="设置允许的最大层级，默认无限制" %>
<%@ attribute name="className" type="java.lang.String" description="设置css样式" %>
<%@ attribute name="nodeId" type="java.lang.String" description="配置映射关系：设置节点id对应在json中的哪个key，默认为id" %>
<%@ attribute name="parentId" type="java.lang.String" description="配置映射关系：设置节点pid对应在json中的哪个key，默认为pid" %>
<%@ attribute name="dataStructure" type="java.lang.String" description="返回数据的数据结构，可选值：tree/array，默认为array" %>
<%@ attribute name="rootPidValue" type="java.lang.String" description="当设置dataStructure=array时，可设置此属性，此属性指返回的数据的根节点的值，默认为null，" %>
<%@ attribute name="silent" type="java.lang.Boolean" description="刷新时，是否静默刷新，默认为true" %>
<%@ attribute name="onAsyncError" type="java.lang.String" description="用于捕获异步加载出现异常错误的事件回调函数，函数参数onAsyncError(event,treeId,treeNode,XMLHttpRequest,textStatus,errorThrown)" %>
<%@ attribute name="onAsyncSuccess" type="java.lang.String" description="用于捕获异步加载正常结束的事件回调函数，函数参数 onAsyncSuccess(event, treeId, treeNode, msg)" %>
<div class='m-loader-wrapper ${className }'>
	<ul class="ztree" id="${id}"></ul>
</div>
<script type="text/javascript">
(function(){
	var ajax = window["${ajax}"];
	var succFunc = ajax.succFunc;
	
	// 属性
	var showRemoveBtn = "${showRemoveBtn}"=="" ? false : "${showRemoveBtn}";
	var canEdit = "${canEdit}"=="" ? false : "${canEdit}";
	var checkable = "${checkable}"=="" ? false : "${checkable}";
	var showLine = "${showLine}"=="" ? false : "${showLine}";
	var selectedMulti = "${selectedMulti}"=="" ? false : "${selectedMulti}";
	var dblClickExpand = "${dblClickExpand}"=="" ? false : "${dblClickExpand}";
	var defaultSelectIndex = "${defaultSelectIndex}" == "" ? 0 :"${defaultSelectIndex}";
	
	var currentNodeSelectedFunc = "${onCurrentNodeSelected}" == "" ? undefined : eval("${onCurrentNodeSelected}");
	var mutiNodesSelectedFunc = "${onMutiNodesSelected}" == "" ? undefined : eval("${onMutiNodesSelected}");
	var converToTemplateFunc = "${convertToTemplate}" == "" ? undefined : eval("${convertToTemplate}");
	var onAsyncSuccess = "${onAsyncSuccess}" == "" ? undefined : eval("${onAsyncSuccess}");
	var onAsyncSuccess = "${onAsyncError}" == "" ? undefined : eval("${onAsyncError}");
	
	var nodeNameKey = "${nodeNameKey}"||"text";
	var urlKey = "${nodeUrlKey}" || "url";
	var titleKey = "${nodeTitleKey}" || "title";
	var childrenKey = "${nodeChildrenKey}" || "children";
	var checkedKey = "${nodeCheckedKey}" || "checked";
	
	var nodeId = "${nodeId}"||"id";
	var parentId = "${parentId}"||"pid";
	var rootPidValue = "${rootPidValue}" || null;
	
	//实例化一个树的对象
	var treeId = "${id}";
	var keys = {
		name: nodeNameKey,
		url: urlKey,
		title: titleKey,
		checked: checkedKey,
		children: childrenKey,
		id:nodeId,
		pid:parentId
	};
	var opt = {};
	opt.tree = window[treeId] = new Tree(treeId,keys);
	opt.tree.setMaxLayer('${maxLayer}');
	opt.tree.onCurrentNodeSelected = currentNodeSelectedFunc;
	if(converToTemplateFunc!=undefined)
		opt.tree.converNodeToTemplate = converToTemplateFunc;
			
	ajax.bindSuccessEvent(function(odata,status,xhr){
		$('#${id}').closest('.m-loader-wrapper').stopLoading();
		// 数据处理
		var data = null;
		if (!(odata instanceof Array)) {
			data = odata.info;
		} else {
		    data = odata;
		}
		if ('${dataStructure}' == 'array') {
			// Logger.info('tree = > ' + JSON.stringify(data));
			data = $.toTree({
				"data":data,
				"rootPidValue":rootPidValue,
				"children":childrenKey,
				"contentDefine":{
					"id":"${nodeId}",
					"pid":"${parentId}"
				}
			});
		}
		var setting = {
			view : {
				dblClickExpand : dblClickExpand,
				showLine : showLine,
				selectedMulti : selectedMulti
			},
			data : {
				keep : {
					parent : true,
					leaf : false
				},
				simpleData : {
					enable : true,
					/* idKey : "id",
					pIdKey : "pid", */
					idKey : nodeId,
					pIdKey : parentId,
					rootPId : ""
				},
				key:{
					name: nodeNameKey,
					url: urlKey,
					title: titleKey,
					checked: checkedKey,
					children: childrenKey,
					
					id:nodeId,
					pid:parentId
				}
			},
			edit : {
				enable : canEdit,
				showRemoveBtn : showRemoveBtn,
			},
			callback : {
				'onClick' : function() { // 点击显示节点到内容
					var zTree = $.fn.zTree.getZTreeObj(treeId);
					var nodes = zTree.getSelectedNodes();
					mutiNodesSelectedFunc && mutiNodesSelectedFunc(nodes);
					currentNodeSelectedFunc && currentNodeSelectedFunc(nodes[0]);
					return nodes;
				},
				'onAsyncError': function(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
					onAsyncError && onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown);
				},
				'onAsyncSuccess' :function(event, treeId, treeNode, msg) {
					onAsyncSuccess && onAsyncSuccess(event, treeId, treeNode, msg);
				},
				'onDblClick':function(event, treeId, treeNode) {
					
				}
			},
		
			check : { // 复选框属性
				chkStyle : "checkbox",
				enable : checkable, 
				chkboxType : { "y" :"ps", "N" : "ps"}
			}
		};
		if (ArrayUtil.hasData(data)) {
            data[defaultSelectIndex].open = 'true';// 展开首页
		}
		$.fn.zTree.init($("#"+treeId), setting, data);
		succFunc && succFunc(odata,status,xhr);
	});
	
	var oAjaxErrorFunc = ajax.errorFunc;
	ajax.bindErrorEvent(function(xhr,textStatus,errorThrown){
		$('#${id}').closest('.m-loader-wrapper').stopLoading();
		oAjaxErrorFunc && oAjaxErrorFunc(xhr,textStatus,errorThrown);
	});
	var oAjaxCompleteFunc = ajax.completeFunc;
	ajax.bindCompleteSendEvent(function(xhr,textStatus){
		$('#${id}').closest('.m-loader-wrapper').stopLoading();
		oAjaxCompleteFunc && oAjaxCompleteFunc(xhr,textStatus);
	});
	var onAjaxBeforeSendFunc = ajax.beforeSendFunc;
	ajax.bindBeforeSendEvent(function(xhr){
		if ('${silent}'=='false') {
			$('#${id}').closest('.m-loader-wrapper').startLoading();
		}
		onAjaxBeforeSendFunc && onAjaxBeforeSendFunc(xhr);
	});
	ajax.send();
})();
</script>